package shiroTest;

import java.util.Collection;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.junit.Test;

import junit.framework.Assert;

public class ShiroTest01 {
	protected void login(String configFile, String username, String password) {
        //1、获取SecurityManager工厂，此处使用Ini配置文件初始化SecurityManager
        Factory<org.apache.shiro.mgt.SecurityManager> factory =
                new IniSecurityManagerFactory(configFile);

        //2、得到SecurityManager实例 并绑定给SecurityUtils
        org.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance();
        SecurityUtils.setSecurityManager(securityManager);

        //3、得到Subject及创建用户名/密码身份验证Token（即用户身份/凭证）
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);

        subject.login(token);
    }
	public Subject subject() {
        return SecurityUtils.getSubject();
    }
	@Test
	public void testShiro(){
		
		try {
			login("classpath:shiro-jdbc-realm.ini","zhang","123");
			System.out.println("登录成功");
		} catch (Exception e) {
//			e.printStackTrace();
			System.out.println("登录失败");
		}
		
/*		Collection collection = subject().getSession().getAttributeKeys();
		
		for(Object i : collection){
			System.out.println(i.toString());
			System.out.println(subject().getSession().getAttribute(i.toString()));
		}
		*/
		System.out.println(subject().isPermitted("user:chishi"));
		
	}
	
	/*public static void main(String[] args) {
		login();
		
	}
*/
}
